import { createRouter, createWebHistory } from "vue-router";
import type { RouteRecordRaw } from "vue-router";

// Define route meta types
declare module "vue-router" {
  interface RouteMeta {
    title?: string;
    icon?: string;
    requiresAuth?: boolean;
    isImplemented?: boolean;
  }
}

// Define routes
const routes: RouteRecordRaw[] = [
  {
    path: "/",
    component: () => import("../layouts/MainLayout.vue"),
    children: [
      {
        path: "",
        name: "home",
        component: () => import("../views/HomeView.vue"),
        meta: {
          title: "首页",
          icon: "🏠",
        },
      },
      {
        path: "category/:id",
        name: "category",
        component: () => import("../views/CategoryView.vue"),
        meta: {
          title: "实验分类",
        },
      },
      {
        path: "experiments/spectrometer",
        name: "spectrometer",
        component: () => import("../views/experiments/SpectrometerView.vue"),
        meta: {
          title: "分光计实验",
          icon: "🔍",
          isImplemented: true,
        },
      },
      {
        path: "experiments/bridge",
        name: "bridge",
        component: () => import("../views/experiments/BridgeView.vue"),
        meta: {
          title: "自组电桥测电阻实验",
          icon: "⚡",
          isImplemented: true,
        },
      },
      {
        path: "dev/template-demo",
        name: "template-demo",
        component: () => import("../views/dev/TemplateDemoView.vue"),
        meta: {
          title: "组件模板演示",
          icon: "📝",
          isImplemented: true,
        },
      },
      {
        path: "dev/element-demo",
        name: "element-demo",
        component: () => import("../views/dev/ElementDemoView.vue"),
        meta: {
          title: "Element Plus 组件演示",
          icon: "🧩",
          isImplemented: true,
        },
      },
      {
        path: "experiments/fiber-optic-communication",
        name: "fiber-optic-communication",
        component: () => import("../views/ComingSoonView.vue"),
        meta: {
          title: "光纤通信实验",
          icon: "📡",
          isImplemented: false,
        },
      },
      {
        path: "experiments/thin-film-refraction",
        name: "thin-film-refraction",
        component: () => import("../views/ComingSoonView.vue"),
        meta: {
          title: "测量透明薄片的折射率",
          icon: "🔎",
          isImplemented: false,
        },
      },
      {
        path: "experiments/fiber-sensor",
        name: "fiber-sensor",
        component: () => import("../views/ComingSoonView.vue"),
        meta: {
          title: "光纤传感器实验",
          icon: "🎚️",
          isImplemented: false,
        },
      },
      {
        path: "experiments/polarization",
        name: "polarization",
        component: () => import("../views/ComingSoonView.vue"),
        meta: {
          title: "光的偏振实验",
          icon: "↔️",
          isImplemented: false,
        },
      },
      {
        path: "experiments/michelson-interferometer",
        name: "michelson-interferometer",
        component: () => import("../views/ComingSoonView.vue"),
        meta: {
          title: "迈克尔逊干涉仪",
          icon: "🔆",
          isImplemented: false,
        },
      },
      {
        path: "experiments/telescope-microscope",
        name: "telescope-microscope",
        component: () => import("../views/ComingSoonView.vue"),
        meta: {
          title: "自组望远镜和显微镜",
          icon: "🔭",
          isImplemented: false,
        },
      },
      {
        path: "experiments/equal-thickness-interference",
        name: "equal-thickness-interference",
        component: () => import("../views/ComingSoonView.vue"),
        meta: {
          title: "等厚干涉实验",
          icon: "🌈",
          isImplemented: false,
        },
      },
      {
        path: "experiments/lens-focal-length",
        name: "lens-focal-length",
        component: () => import("../views/ComingSoonView.vue"),
        meta: {
          title: "薄透镜焦距的测定",
          icon: "👁️",
          isImplemented: false,
        },
      },
      {
        path: "experiments/potentiometer",
        name: "potentiometer",
        component: () => import("../views/ComingSoonView.vue"),
        meta: {
          title: "电位差计实验",
          icon: "🔋",
          isImplemented: false,
        },
      },
      {
        path: "experiments/electron-beam",
        name: "electron-beam",
        component: () => import("../views/ComingSoonView.vue"),
        meta: {
          title: "电子束的偏转和电子比荷的测定",
          icon: "💫",
          isImplemented: false,
        },
      },
      {
        path: "experiments/electrostatic-field",
        name: "electrostatic-field",
        component: () => import("../views/ComingSoonView.vue"),
        meta: {
          title: "用模拟法测绘静电场实验",
          icon: "⚪",
          isImplemented: false,
        },
      },
      {
        path: "experiments/magnetic-hysteresis",
        name: "magnetic-hysteresis",
        component: () => import("../views/ComingSoonView.vue"),
        meta: {
          title: "动态磁滞回线的测量",
          icon: "🧲",
          isImplemented: false,
        },
      },
      {
        path: "experiments/magnetic-field-measurement",
        name: "magnetic-field-measurement",
        component: () => import("../views/ComingSoonView.vue"),
        meta: {
          title: "磁场的测量",
          icon: "📊",
          isImplemented: false,
        },
      },
      {
        path: "experiments/meter-modification",
        name: "meter-modification",
        component: () => import("../views/ComingSoonView.vue"),
        meta: {
          title: "电表的改装和校准",
          icon: "🔌",
          isImplemented: false,
        },
      },
      {
        path: "experiments/:id",
        name: "experiment-coming-soon",
        component: () => import("../views/ComingSoonView.vue"),
        meta: {
          title: "实验开发中",
          isImplemented: false,
        },
      },
    ],
  },
  {
    path: "/:pathMatch(.*)*",
    name: "not-found",
    component: () => import("../views/NotFoundView.vue"),
    meta: {
      title: "404 Not Found",
    },
  },
];

// Create router instance
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  },
});

// Navigation guards
router.beforeEach((to, from, next) => {
  // Update document title
  if (to.name === "category" && to.params.id) {
    // For category pages, try to set the title based on the category name
    import("./categories")
      .then(({ getCategoryName }) => {
        const categoryName = getCategoryName(to.params.id as string);
        document.title = `${categoryName || "实验分类"} - 物理实验数据处理平台`;
      })
      .catch(() => {
        document.title = "实验分类 - 物理实验数据处理平台";
      });
  } else {
    document.title = `${to.meta.title ? to.meta.title + " - " : ""}物理实验数据处理平台`;
  }

  // Add more navigation guards as needed
  next();
});

export default router;
